package com.hxc.shop.service;

import com.hxc.shop.bean.ShopGoodsInfo;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.GoodsCategoryMapper;
import com.hxc.shop.mapper.Mysql.GoodsInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
public class GoodsInfoService {
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    public ResultVO addGoods(ShopGoodsInfo goodsInfo) {
        if (goodsInfo == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品数据为空,新增商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsName())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品名称为空,新增商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsDescription())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品描述为空,新增商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsPrice())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品价格为空,新增商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsCategoryId())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类为空,新增商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsInventoryCount())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品库存数量为空,新增商品失败!");
        }
        if (goodsInfo.getGoodsInventoryCount() == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(), "商品课程数量为0,新增商品失败!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        goodsInfo.setCreateTime(sdf.format(new Date()));
        goodsInfo.setUpdateTime(sdf.format(new Date()));

        boolean checkSpecialChar = isSpecialChar(goodsInfo.getGoodsCategoryId());
        if (checkSpecialChar == true) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类包含除,以外的特殊字符!");
        }

        String[] categoryList = goodsInfo.getGoodsCategoryId().split(",");
        for (int i = 0; i< categoryList.length;i++) {
            Integer checkId = 0;
            try {
                checkId = goodsCategoryMapper.getCategoryIdByCategoryId(Integer.valueOf(categoryList[i]));
            }catch (Exception e) {
                log.error("获取商品分类编号失败:"+categoryList[i]+"=="+e.getMessage());
                return new ResultVO(ResultCode.ERROR.getCode(),"查找商品分类编号失败:"+e.getMessage());
            }
            if (checkId == null || checkId == 0) {
                return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品分类编号,商品分类编号为:"+categoryList[i]);
            }
        }

        try {
            goodsInfoMapper.insertGoods(goodsInfo);
        }catch (Exception e) {
            log.error("新增商品失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"新增商品失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"商品名称为:"+goodsInfo.getGoodsName()+"的商品新增成功!");
    }

    public ResultVO updateGoods(ShopGoodsInfo goodsInfo) {
        if (goodsInfo == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品数据为空,更新商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getId())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到商品编号，更新商品数据失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsName())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品名称为空,更新商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsDescription())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品描述为空,更新商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsPrice())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品价格为空,更新商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsCategoryId())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类为空,更新商品失败!");
        }
        if (StringUtils.isEmpty(goodsInfo.getGoodsInventoryCount())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品库存数量为空,更新商品失败!");
        }
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(goodsInfo.getId());
        }catch (Exception e) {
            log.error("更新商品数据-查找商品编号失败:"+goodsInfo.getId()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号!");
        }


        String[] categoryList = goodsInfo.getGoodsCategoryId().split(",");
        for (int i = 0; i< categoryList.length;i++) {
            Integer checkId = 0;
            try {
                checkId = goodsCategoryMapper.getCategoryIdByCategoryId(Integer.valueOf(categoryList[i]));
            }catch (Exception e) {
                log.error("获取商品分类编号失败:"+categoryList[i]+"=="+e.getMessage());
                return new ResultVO(ResultCode.ERROR.getCode(),"查找商品分类编号失败:"+e.getMessage());
            }
            if (checkId == null || checkId == 0) {
                return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品分类编号,商品分类编号为:"+categoryList[i]);
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        goodsInfo.setUpdateTime(sdf.format(new Date()));
        try {
            goodsInfoMapper.updateGoods(goodsInfo);
        }catch (Exception e) {
            log.error("更新商品数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"更新商品编号:"+goodsInfo.getId()+"的商品成功!");
    }

    public ResultVO getGoodsByRand(Integer count) {
        List<ShopGoodsInfo> shopGoodsInfoList = new ArrayList<>();
        try {
            shopGoodsInfoList = goodsInfoMapper.getGoodsByRand(count);
        }catch (Exception e) {
            log.error("获取商品失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsInfoList);
    }

    public ResultVO getGoodsById(Integer id) {
        if (id == null || id == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空,获取商品数据失败!");
        }
        // 判断商品编号是否存在
        Integer checkId = 0;
        try {
            checkId = goodsInfoMapper.findGoodsIdByGoodsId(id);
        }catch (Exception e) {
            log.error("查找商品编号失败:"+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号，获取商品数据失败!");
        }
        if (checkId == null || checkId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号，获取商品数据失败!");
        }
        ShopGoodsInfo shopGoodsInfo = new ShopGoodsInfo();
        try {
            shopGoodsInfo = goodsInfoMapper.getGoodsById(id);
        }catch (Exception e) {
            log.error("获取商品数据失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品数据失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsInfo);
    }

    public ResultVO searchGoodsByGoodsName(String goodsName) {
        if (StringUtils.isEmpty(goodsName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品名称为空,查找商品失败!");
        }
        List<ShopGoodsInfo> shopGoodsInfoList = new ArrayList<>();
        try {
            shopGoodsInfoList = goodsInfoMapper.searchGoodsByGoodsName("%"+goodsName+"%");
        }catch (Exception e) {
            log.error("搜索商品信息失败:"+goodsName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"搜索商品信息失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsInfoList);
    }

    public ResultVO findGoodsByGoodsCategory(String goodCategory){
        if (StringUtils.isEmpty(goodCategory)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类Id为空,查找失败");
        }
        boolean checkSpecialChar = isSpecialChar(goodCategory);
        if (checkSpecialChar == true) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类包含除,以外的特殊字符!");
        }
        String[] categoryList = goodCategory.split(",");
        for (int i = 0;i<categoryList.length;i++) {
            Integer checkId = 0;
            try {
                checkId = goodsCategoryMapper.getCategoryIdByCategoryId(Integer.valueOf(categoryList[i]));
            }catch (Exception e) {
                log.error("查找商品数据-获取商品分类编号失败:"+categoryList[i]+e.getMessage());
                return new ResultVO(ResultCode.ERROR.getCode());
            }
            if (checkId == null || checkId == 0) {
                return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品分类编号!商品分类编号为:"+categoryList[i]);
            }
        }
        Map<String,List<ShopGoodsInfo>> data = new HashMap<>();
        for (int i = 0; i<categoryList.length;i++) {
            // 根据商品分类编号获取商品分类名称
            String categoryName = "";
            try {
                categoryName = goodsInfoMapper.getGoodsCategoryNameByGoodsCategoryId(categoryList[i]);
            }catch (Exception e) {
                log.error("商品分类编号获取商品分类名称失败:"+categoryList[i]+"=="+e.getMessage());
                continue;
            }
            // 根据商品分类编号随机获取20个对应分类商品
            List<ShopGoodsInfo> shopGoodsInfoList = new ArrayList<>();
            Integer count = 20;
            try {
                shopGoodsInfoList = goodsInfoMapper.getGoodsInfoByCategoryId(categoryList[i],count);
            }catch (Exception e) {
                log.error("根据商品分类编号随机获取20个对应分类商品失败:"+e.getMessage());
                continue;
            }
            data.put(categoryName,shopGoodsInfoList);
        }

        return new ResultVO(ResultCode.SUCCESS,data);
    }

    public ResultVO findGoodsByGoodsCategoryId(String categoryId) {
        if (StringUtils.isEmpty(categoryId)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到商品分类id，查找商品失败!");
        }
        Integer checkId = 0;
        try {
            checkId = goodsCategoryMapper.getCategoryIdByCategoryId(Integer.valueOf(categoryId));
        }catch (Exception e) {
            log.error("通过单个商品分类编号获取商品分类编号失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取商品分类编号失败:"+e.getMessage());
        }
        if (checkId == null || checkId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品分类编号："+categoryId);
        }
        // 根据商品分类编号随机获取20个对应分类商品
        List<ShopGoodsInfo> shopGoodsInfoList = new ArrayList<>();
        Integer count = 20;
        try {
            shopGoodsInfoList = goodsInfoMapper.getGoodsInfoByCategoryId(categoryId,count);
        }catch (Exception e) {
            log.error("根据商品分类编号随机获取20个对应分类商品失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类编号获取商品失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsInfoList);
    }

    public ResultVO findGoodsByGoodsCategoryName(String goodsCategoryName) {
        if (StringUtils.isEmpty(goodsCategoryName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品分类名称为空，查找商品失败!");
        }
        // 根据商品分类名称查找商品分类id
        List<String> categoryIdList = new ArrayList<>();
        try {
            categoryIdList = goodsInfoMapper.findCategoryIdListBygoodsCategoryName(goodsCategoryName);
        }catch (Exception e) {
            log.error("查找商品分类id失败:"+goodsCategoryName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品分类编号失败:"+e.getMessage());
        }
        if (categoryIdList.size()==0) {
            return new ResultVO(ResultCode.ERROR.getCode(), "未查找到商品分类编号!");
        }
        List<ShopGoodsInfo> shopGoodsInfoList = new ArrayList<>();
        Integer count = 20;
        for (String category:categoryIdList) {
            List<ShopGoodsInfo> shopGoodsInfos = new ArrayList<>();
            try {
                shopGoodsInfos = goodsInfoMapper.getGoodsInfoByCategoryId(category,count);
            }catch (Exception e) {
                log.error("获取商品数据失败:"+category+"=="+e.getMessage());
                continue;
            }
            if (shopGoodsInfos.size()!=0) {
                shopGoodsInfoList.addAll(shopGoodsInfos);
            }
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsInfoList);
    }

    public ResultVO deleteGoodsById(Integer id) {
        if (id == null || id == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到商品编号!");
        }
        Integer checkId = 0;
        try {
            checkId = goodsInfoMapper.findGoodsIdByGoodsId(id);
        }catch (Exception e) {
            log.error("删除商品数据查找商品编号失败:"+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+id);
        }
        if (checkId == null || checkId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号："+id);
        }
        try {
            goodsInfoMapper.deleteGoodsById(id);
        }catch (Exception e) {
            log.error("删除商品失败:"+id+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"删除商品失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"删除成功!商品编号:"+id);
    }

    /**
     * 判断是否含有特殊字符
     * @param str 校验的字符串
     * @return true为包含，false为不包含
     */
    public static boolean isSpecialChar(String str) {
        boolean flag = false;
        flag=str.matches("[0-9]+");
        if (flag == true) {
            flag = false;
        } else {
            // 不是纯数字,判断是否只包含,
            String[] strSplit = str.split(",");
            for (int i = 0;i<strSplit.length;i++){
                flag= strSplit[i].matches("[0-9]+");
                if (flag == false) {
                    flag = true;
                    break;
                } else {
                    flag = false;
                }
            }
        }


        return flag;
    }


}
